package com.base.common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.base.common.DBAccess;
import com.base.model.CompanyInfo;
import com.base.model.Knowledge;

public class CompanyDAO {
	
	private DBAccess dbAccess = new DBAccess();

	
	public DBAccess getDbAccess() {
		return dbAccess;
	}


	public void setDbAccess(DBAccess dbAccess) {
		this.dbAccess = dbAccess;
	}
	
	public CompanyInfo load(){
		CompanyInfo info = new  CompanyInfo();
		List<CompanyInfo> resultList = new ArrayList<CompanyInfo>();
		try {
			if(dbAccess.createConn()){
				String sql = "select * from TB_COMPANY_INFO ";
				dbAccess.query(sql);
				if(dbAccess.next()){
					ResultSet rs = dbAccess.getRs();
					CompanyInfo entity = new  CompanyInfo();
					entity.setCompanyName(rs.getString("COMPANY_NAME"));
					entity.setCompanyAddres(rs.getString("COMPANY_ADDRESS"));
					entity.setCompanyDescription(rs.getString("COMPANY_DESCRIPTION"));
					entity.setCompanyEmail(rs.getString("COMPANY_EMAIL"));
					entity.setCompanyFax(rs.getString("COMPANY_FAX"));
					entity.setCompanyId(rs.getString("COMPANY_ID"));
					entity.setCompanyMobile(rs.getString("COMPANY_MOBILE"));
					entity.setCompanyPostcode(rs.getString("COMPANY_POSTCODE"));
					entity.setCompanyTel(rs.getString("COMPANY_TEL"));
					resultList.add(entity);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbAccess.closeRs();
			dbAccess.closeStm();
			dbAccess.closeConn();
		}
		if(resultList.size() > 0){
			info = resultList.get(0);
		}
		return info;
	}
	
	public boolean update(CompanyInfo info){
		boolean result = false;
		try {
			if(dbAccess.createConn()){
				String sql = "update TB_COMPANY_INFO "+
						"set COMPANY_NAME = '" + info.getCompanyName() + "', " +
								"COMPANY_ADDRESS = '" + info.getCompanyAddres() + "', " +
								"COMPANY_DESCRIPTION = '" + info.getCompanyDescription() + "', " +
								"COMPANY_EMAIL = '" + info.getCompanyEmail() + "', " +
								"COMPANY_FAX = '" + info.getCompanyFax() + "', " +
								"COMPANY_MOBILE = '" + info.getCompanyMobile() + "', " +
								"COMPANY_POSTCODE = '" + info.getCompanyPostcode() + "', " +
								"COMPANY_TEL = '" + info.getCompanyTel() + "' " +
								"where COMPANY_ID ='" + info.getCompanyId() + "'";
				dbAccess.update(sql);
				result = true;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbAccess.closeRs();
			dbAccess.closeStm();
			dbAccess.closeConn();
		}
		return result;
	}
}
